package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	comment := "Hello, World!"
	basicReader := strings.NewReader(comment)
	
	// 缓冲区中未读字节数小于Peek方法指定的n
	reader1 := bufio.NewReader(basicReader)
	peekNum := len(comment) + 1
	bytes, err := reader1.Peek(peekNum)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	fmt.Printf("缓冲区中未读字节数： %d, Peek读取: %d\n", reader1.Buffered(), peekNum)
	fmt.Printf("Peek读取(%d): %q\n", len(bytes), bytes)
	fmt.Println()

	// Peek方法指定的n比缓冲区长度还要大
	basicReader.Reset(comment)
	size := 300
	reader2 := bufio.NewReaderSize(basicReader, size)
	peekNum = size + 1
	bytes, err = reader2.Peek(peekNum)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
	}
	fmt.Printf("缓冲区长度: %d, Peek读取: %d\n", size, peekNum)
	fmt.Printf("Peek读取(%d): %q\n", len(bytes), bytes)
}
